<template>
	<view class="content">
		<u-popup :show="show" mode="center">
			<view>
				<text>欢迎进入聊天室</text>
				<view>用户名</view><input placeholder="请输入用户名" v-model="username"/>
				<button @click="login()">登录</button>
			</view>
		</u-popup>
		
		<view>欢迎{{username}}</view>
		<view>添加好友</view>
		<button @click="showAddFriends = true">+</button>
		
		<view style="width: 100%;text-align: center;">
			<view style="margin-top: 30rpx;height: 80rpx;width: 100%;line-height: 80rpx;
			border-top: 1rpx solid black;border-bottom: 1rpx solid black;"
			v-for="(conversation,index) in conversations" :Key="index" 
			@click="toIm(conversation.username1 == username ?
				conversation.username2:conversation.username1,conversation.id)">
				 
				<view>好友{{conversation.username1 == username ?
				 conversation.username2:conversation.username1}}</view>
			</view>
		</view>
		
		<u-popup :show="showAddFriends" mode="center">
			<view>
				<text>添加好友</text>
				<view>用户名</view><input placeholder="请输入好友用户名" v-model="friendUsername"/>
				<button @click="addFriends(friendUsername)">添加</button>
			</view>
		</u-popup>
		
		<u-popup :show="showFriendsAddMe" mode="center">
			<view>
				<view>用户{{addMeName}}请求添加你为好友</view>
				<button @click="agree()">同意</button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				conversations: [],
				username: '',
				friendUsername: '',
				addMeName: '',
				showAddFriends: false,
				showFriendsAddMe: false
				
			}
		},
		onLoad() {
			this.isLogin()
			this.getConversation()
			this.webSocketConnect()
		},
		onShow() {
			// this.webSocketConnect()
		},
		onHide() {
			// uni.closeSocket()
		},
		onUnload() {
			uni.closeSocket()
		},
		methods: {
			isLogin() {
				uni.getStorage({
					key: 'username',
					success: (res) => {
						// console.log(res)
						this.show = false
						this.username = res.data
					},
					fail: () => {
						this.show = true
					}
				})
			},
			webSocketConnect() {
				uni.connectSocket({
					url: 'ws://10.152.174.195/im_api/addFriends/' + this.username,
					success: () => {
						console.log('连接成功')
					}
				})
				uni.onSocketOpen(() => {
					console.log('lianjie')
				})
				uni.onSocketMessage((res) => {
				this.addMeName = res.data
				this.showFriendsAddMe = true
				  console.log('收到服务器内容：' + res.data);
				})
			},
			getConversation() {
				uni.request({
					url: 'http://10.152.174.195/conversation/getConversations/' + this.username,
					success: (res) => {
						this.conversations = res.data
						console.log(this.conversations)
					}
				})
			},
			login() {
				this.show = false
				uni.setStorage({
					key: 'username',
					data: this.username
				})
			},
			addFriends(friendUsername) {
				this.friendUsername = ''
				this.showAddFriends = false
				uni.sendSocketMessage({
				    data: friendUsername
				})
				this.getConversation()
				
			},
			agree() {
				this.showFriendsAddMe = false
				this.getConversation()
			},
			toIm(friendUsername,id) {
				// console.log(friendUsername)
				uni.navigateTo({
					url: '/pages/imPage/imPage?friendUsername=' + friendUsername 
					+ '&conversationId=' + id
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
